﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Globalization;

namespace TallComponents.Web.Pdf
{
   /// <summary>
   /// Represents request parameters.
   /// </summary>
   internal abstract class RequestParameters
   {
      //Image type - specifies the intent of the generated image. 
      //It could be an image for the viewer, for the thumbnails and so on.
      internal const string IMAGETYPE = "it";
      //internal const string PAGEINDEXKEY = "a";
      internal const string FIRSTPAGEINDEXKEY = "a";
      internal const string FIRSTPAGEDYKEY = "y";
      internal const string FIRSTPAGEDXKEY = "m";
      internal const string LASTPAGEINDEXKEY = "p";
      internal const string UNIQUEKEY = "b";
      internal const string SCALEKEY = "c";
      internal const string DXKEY = "d";
      internal const string DYKEY = "e";
      internal const string WIDTHKEY = "f";
      internal const string HEIGHTKEY = "g";
      //internal const string MAXVERTICALPAGESKEY = "v";
      //internal const string QUALITYKEY = "q";
      internal const string ROTATIONKEY = "r";
      internal const string VERTICALGAPKEY = "j";
      internal const string HORISONTALGAPKEY = "h";
      internal const string PAGELAYOUTKEY = "l";
      internal const string LAYOUTINSTRUCTIONSKEY = "x";
      internal const string MODIFICATIONDATE = "fv";


      public string Key { get; private set; }
      public int FirstPageIndex { get; private set; }
      public int FirstPageDy { get; private set; }
      public int FirstPageDx { get; private set; }
      public int LastPageIndex { get; private set; }
      public int Width { get; private set; }
      public int Height { get; private set; }
      public float ZoomPercentage { get; private set; }
      public int Dx { get; private set; }
      public int Dy { get; private set; }
      public bool HighQuality { get; private set; }
      //public int MaxVerticalPages { get; private set; }
      public int VerticalGap { get; private set; }
      public int HorisontalGap { get; private set; }

      //public PageLayout PageLayout { get; private set; }

      /// <summary>
      /// Initializes a new instance of the <see cref="RequestParameters"/> class.
      /// </summary>
      /// <param name="values">The values.</param>
      public RequestParameters(NameValueCollection values)
      {
         Key = values[UNIQUEKEY];
         FirstPageIndex = GetIntFromParam(values[FIRSTPAGEINDEXKEY]);
         LastPageIndex = GetIntFromParam(values[LASTPAGEINDEXKEY]);
         Width = GetIntFromParam(values[WIDTHKEY]);
         Height = GetIntFromParam(values[HEIGHTKEY]);
         ZoomPercentage = Convert.ToSingle(values[SCALEKEY], CultureInfo.InvariantCulture);
         Dx = GetIntFromParam(values[DXKEY]);
         Dy = GetIntFromParam(values[DYKEY]);
         FirstPageDy = GetIntFromParam(values[FIRSTPAGEDYKEY]);
         FirstPageDx = GetIntFromParam(values[FIRSTPAGEDXKEY]);
         VerticalGap = GetIntFromParam(values[VERTICALGAPKEY]);
         HorisontalGap = GetIntFromParam(values[HORISONTALGAPKEY]);

         HighQuality = true; //!string.Equals( values[ QUALITYKEY ], "low", StringComparison.OrdinalIgnoreCase );

         //PageLayout = (PageLayout)GetIntFromParam(values[PAGELAYOUTKEY]);
      }

      /// <summary>
      /// Determines whether request is short.
      /// </summary>
      /// <param name="values">The values.</param>
      /// <returns>
      ///   <c>true</c> if request is short; otherwise, <c>false</c>.
      /// </returns>
      public static bool IsShortRequest(NameValueCollection values)
      {
         return (string.IsNullOrEmpty(values[DXKEY]) && string.IsNullOrEmpty(values[DYKEY]) &&
                 string.IsNullOrEmpty(values[WIDTHKEY]) && string.IsNullOrEmpty(values[HEIGHTKEY]));
      }

      /// <summary>
      /// Gets the int from param.
      /// </summary>
      /// <param name="param">The param.</param>
      /// <returns></returns>
      internal static int GetIntFromParam(string param)
      {
         int result = 0;
         if (!string.IsNullOrEmpty(param) && (!param.Equals("NaN")))
         {
            if (param.EndsWith("px"))
               param = param.Substring(0, param.Length - 2);

            if (!int.TryParse(param.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out result))
            {
               double doubleValue;
               if (double.TryParse(param.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out doubleValue))
               {
                  result = Convert.ToInt32(doubleValue);
               }
            }
         }
         return result;
      }

      /// <summary>
      /// Normalizes the rotation.
      /// </summary>
      /// <param name="rotation">The rotation.</param>
      /// <returns></returns>
      public static int NormalizeRotation(int rotation)
      {
         int normalizedRotation = rotation < 0 ? (rotation % 360) + 360 : (rotation % 360);
         return (int)(Math.Round(normalizedRotation / 90D)) * 90;
      }
   }
}
